
layui.use(['form','layer','jquery'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer
        $ = layui.jquery;

    //登录按钮
    form.on("submit(regist)",function(data){
        $(this).text("注册中...").attr("disabled","disabled").addClass("layui-disabled");
        let _this = $(this);
        let params = {};
        let username = $("#userName").val();
        let password = $("#passWord").val();
        let confirmPassWord = $("#confirmPassWord").val();
        if (password!== confirmPassWord){
            layer.msg("两次密码输入不一致！")
            _this.text("注册").removeAttr("disabled").removeClass("layui-disabled");
            return false;
        }
        params.userName = username;
        params.passWord = password;
        $.ajax({
            url: API_BASE_URL+'/admin?method=doRegist',
            method:"POST",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            data:JSON.stringify(params),
            success(res){
                if (res.code == 200){
                    layer.msg('注册成功,2秒后跳转登录页');
                    setTimeout(function (){
                        window.location.href = "admin?method=toLogin";
                    },2000)
                }else {
                    layer.msg('注册失败');
                    _this.text("注册").removeAttr("disabled").removeClass("layui-disabled");
                }
            }
        })
        return false;
    })

    //表单输入效果
    $(".loginBody .input-item").click(function(e){
        e.stopPropagation();
        $(this).addClass("layui-input-focus").find(".layui-input").focus();
    })


    $(".userName").blur(function (){
        let params = {};
        let username = $("#userName").val();
        params.userName = username;
        $.ajax({
            url: API_BASE_URL + '/admin?method=checkUsername',
            method: "POST",
            data: params,
            success(res) {
                if (res.code != 200) {
                    layer.msg(res.message);
                }
            }
        })
    })

    form.verify({
        confirmPwd : function(val){
            let confirmPassWord = $("#confirmPassWord").val();
            if(val !== confirmPassWord){
                return "两次输入密码不一致！";
            }
        } ,pass: [
            /^[\S]{6,12}$/
            ,'密码必须6到12位，且不能出现空格'
        ]
    })

    $(".loginBody .layui-form-item .layui-input").focus(function(){
        $(this).parent().addClass("layui-input-focus");
    })
    $(".loginBody .layui-form-item .layui-input").blur(function(){
        $(this).parent().removeClass("layui-input-focus");
        if($(this).val() != ''){
            $(this).parent().addClass("layui-input-active");
        }else{
            $(this).parent().removeClass("layui-input-active");
        }
    })
})
